import 'package:flutter/material.dart';
import 'package:flutter_boss_mine/utils/color_util.dart';
import 'package:flutter_boss_mine/widget/mine_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:random_color/random_color.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MinePage extends StatefulWidget {
  @override
  _MinePageState createState() => _MinePageState();


}

class _MinePageState extends State<MinePage> with AutomaticKeepAliveClientMixin {
  String _name = "";
  ScrollController _controller = ScrollController();
  double alpha_ad = 1;
  double current_offset = 0;

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }


  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _controller.jumpTo(53);
      // _controller.animateTo(40, duration: Duration(milliseconds: 300), curve: Curves.easeInQuad);
    });

  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: NotificationListener(
          onNotification: (scrollNotication) {
            if (scrollNotication is ScrollUpdateNotification &&
                scrollNotication.depth == 0) {
              _onScroll(scrollNotication.metrics.pixels);
            }
          },
          child: Listener(
            onPointerUp: (e) {
              if (current_offset < 54) {
                _controller.animateTo(54, duration: Duration(milliseconds: 300), curve: Curves.easeInQuad);
              }
            },
            child: Stack(
              children: [
                CustomScrollView(
                  controller: _controller,
                  slivers: [
                    SliverAppBar(
                      expandedHeight: 340.0,
                      //合并的高度，默认是状态栏的高度加AppBar的高度
                      floating: false,
                      //滑动时是否悬浮
                      pinned: true,
                      //标题栏是否固定
                      snap: false,
                      //配合floating使用
                      backgroundColor: ColorUtil.color("#23ACA8"),
                      forceElevated: false,
                      //阴影
                      flexibleSpace: FlexibleSpaceBar(
                        title: Text(_name),
                        titlePadding: EdgeInsets.only(left: 10, bottom: 15),
                        centerTitle: false,
                        collapseMode: CollapseMode.parallax,
                        background: Container(
                            child: Column(
                              children: [
                                SizedBox(
                                  height: 80,
                                ),
                                _editWidget(),
                                SizedBox(
                                  height: 10,
                                ),
                                _headerItems(),
                                _addWidget(),
                              ],
                            )),
                      ),
                      actions: [
                        IconButton(
                          icon: Image.asset(
                              "resource/images/ic_action_scan_white.png",
                              width: 25),
                          onPressed: () {
                            print("点击了扫描二维码");
                          },
                        ),
                        IconButton(
                          onPressed: () {
                            print("点击了设置");
                          },
                          icon: Image.asset(
                              "resource/images/ic_action_settings_white.png",
                              width: 25),
                        )
                      ],
                    ),

                    SliverFixedExtentList(
                      itemExtent: 800,
                      delegate: SliverChildBuilderDelegate(
                            (context, index) {
                          return _bottomView();
                        },
                        childCount: 1,
                      ),
                    ),
                  ],
                ),


              ],
            )),
          )
    );
  }

  //监听滚动
  _onScroll(offset) {
    double alpha = offset / 100;
    if (offset > 280) {
      setState(() {
        _name = "Money";
      });
    } else {
      setState(() {
        _name = "";
        current_offset = offset;
      });
    }

  }

  /**
   * header  姓名 在线编辑简历 头像
   */
  _editWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          margin: EdgeInsets.only(left: 20),
          height: 100,
          // width: 200,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                child: Text(
                  "Money",
                  style: TextStyle(fontSize: 30, color: Colors.white),
                ),
                padding: EdgeInsets.only(left: 0, top: 0),
              ),
              SizedBox(
                height: 5,
              ),
              GestureDetector(
                onTap: () {
                  print("点击了在线编辑");
                },
                child: Row(
                  children: [
                    Image.asset(
                      "resource/images/icon_edit_write.png",
                      width: 18,
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Text(
                      "我的在线简历",
                      style: TextStyle(fontSize: 12, color: Colors.white),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
        Container(
          margin: EdgeInsets.only(right: 20),
          height: 70,
          width: 70,
          child: GestureDetector(
            child: PhysicalModel(
              color: Colors.transparent,
              clipBehavior: Clip.antiAlias,
              borderRadius: BorderRadius.circular(35),
              child: Image.asset("resource/images/avatar_16.jpg"),
            ),
          ),
        )
      ],
    );
  }

  _headerItems() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        _headerItem("沟通过", "5"),
        _headerItem("待面试", "0"),
        _headerItem("已投递简历", "2"),
        _headerItem("收藏", "0"),
      ],
    );
  }

  _headerItem(String title, String num) {
    return InkWell(
      onTap: () {
        print("点击了" + title);
      },
      child: Column(
        children: [
          Text(
            num ?? "0",
            style: TextStyle(fontSize: 20, color: Colors.white),
          ),
          Text(
            title,
            style: TextStyle(fontSize: 12, color: Colors.white),
          ),
        ],
      ),
    );
  }

  // VIP卡组件
  _addWidget() {
    return Container(
      margin: EdgeInsets.only(top: 55, left: 20, right: 20),
      height: 145.h,
      child: ClipRRect(
        borderRadius: const BorderRadius.vertical(top: Radius.circular(10)),
        child: Container(
          color: ColorUtil.color("#303030"),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(height: 8,),
              Row(
                // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      SizedBox(width: 10,),
                      Image.asset("resource/images/icon_vip.png", width: 60.w,),
                      SizedBox(width: 5,),
                      Text("升级VIP专享超值权益", style: TextStyle(color: ColorUtil.color("#E9AC89")),)
                    ],
                  ),
                  SizedBox(width: 80,),
                  Container(
                    child: Stack(
                      children: [
                        Container(
                          color: ColorUtil.color("#E9B595"),
                          width: 110.w,
                          height: 35.h,
                        ),
                        Text( " 去升级> ", style: TextStyle(fontSize: 13,color: Colors.black87)),
                      ],
                    )
                  )
                ],
              ),
              Container(
                color: Colors.white60,
                height: .5,
                margin: EdgeInsets.only(left: 10, right: 10),
              ),
              SizedBox(height: 10,),
              Text("     7项VIP专属权益，每天仅需3元", style: TextStyle(fontSize: 12, color: ColorUtil.color("#E9AC89")),)
            ],
          ),
        ),
      ),
    );
  }

  // _bottom View
  _bottomView() {
    return Container(
      height: 800,
      child: Column(
        children: [
          _getWorkHelp(),
          _bottomList(),
          Text("客服电话 400-065-5799 工作时间9:30-18:30", style: TextStyle(fontSize: 12, color: Colors.grey),)
        ],
      ),
    );
  }

  // 求职助手
  _getWorkHelp() {
    return Container(
      margin: EdgeInsets.only(left: 20, right: 20),
      child: Column(
        children: [
          SizedBox(height: 20,),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text("求职助手", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),),
              GestureDetector(
                onTap: (){print("你点击了其他/钱包");},
                child: Text("其他/钱包 >", style: TextStyle(fontSize: 14, color: Colors.black54),),
              )
            ],
          ),
          SizedBox(height: 15,),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _cardItem("简历刷新", "提升简历活跃", "resource/images/icon_resumex.png"),
              _cardItem("竞争力分析", "洞悉行业竞争", "resource/images/icon_resumex.png")
            ],
          ),
        ],

      ),
    );
  }

  _cardItem(String topleft, String bottomleft, String imgName) {
    return GestureDetector(
      onTap: () {print("点击了" + topleft);},
      child: Container(
        height: 80,
        width: 330.w,
        child: Card(
          elevation: 2,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(topleft),
                  Text(bottomleft)
                ],
              ),
              Image.asset(imgName, width: 40,)
            ],
          ),
        ),
      )
    );
  }

  //
  List _bottomItemList = [
    {
      "title": "附件简历",
      "underLine": false,
    },
    {
      "title": "管理求职意向",
      "underLine": true
    },
    {
      "title": "我的发现",
      "underLine": false
    },
    {
      "title": "个人主页",
      "underLine": false
    },
    {
      "title": "牛人问答",
      "underLine": true
    },
    {
      "title": "提升简历曝光",
      "underLine": true
    },
    {
      "title": "薪酬查询",
      "underLine": false
    },
    {
      "title": "帮助与反馈",
      "underLine": false
    },
    {
      "title": "我的客服",
      "underLine": false
    },
    {
      "title": "违规账号处理公告",
      "underLine": false
    },
    {
      "title": "关于",
      "underLine": true
    },
  ];

  _bottomList() {
    return Container(
        height: 600,
        child: ListView.builder(
          physics: NeverScrollableScrollPhysics(),
          itemBuilder: (BuildContext context, int index) {
            Map currentItem = _bottomItemList[index];
            return _mineItem(title: currentItem["title"], underLine: currentItem["underLine"]);
          },
          itemCount: _bottomItemList.length,
        ),
      );
  }

  _mineItem({Widget icon, String title, String rightS, Widget rightIcon, bool underLine = false}) {
    return Container(
      child: Column(
        children: [
          ListTile(
            leading: icon??Icon(Icons.error),
            title: Text(title ?? ""),
            trailing: rightIcon??Icon(Icons.chevron_right),
          ),
          underLine?Container(
            height: 1,
            color: Colors.black26,
          ):SizedBox()
        ],
      ),
    );
  }



}
